package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import a66weding.com.jiehuntong.R;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/5/18
 * @description :
 */
public class EditTextWithAutoClear extends EditText implements View.OnFocusChangeListener, TextWatcher {
    private Drawable clearBtn; //输入框右侧的删除按钮

    private boolean isFoucs; //控件焦点
    private boolean isrighthow;

    private Context context;
    private Drawable leftDrawable;
    private DrawableRightListener mRightListener;

    public EditTextWithAutoClear(Context context) {
        this(context, null);
    }

    public EditTextWithAutoClear(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public EditTextWithAutoClear(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        initial();
    }

    private void initial() {
        clearBtn = getCompoundDrawables()[2]; //EditText右侧的删除图标
        if (leftDrawable == null)
            clearBtn = getResources().getDrawable(R.mipmap.title_bar_search_button_clear_background);

            leftDrawable = getResources().getDrawable(R.mipmap.title_bar_search_zoomin);

        clearBtn.setBounds(0, 0, clearBtn.getIntrinsicWidth(), clearBtn.getIntrinsicHeight());
        leftDrawable.setBounds(0, 0, leftDrawable.getIntrinsicWidth(), leftDrawable.getIntrinsicHeight());

        clearText(false); //默认设置隐藏图标
        setOnFocusChangeListener(this); //设置焦点改变的监听
        addTextChangedListener(this); //设置输入框里面内容发生改变的监听

    }



    protected void clearText(boolean visible) {
        Drawable right =clearBtn;

        if(visible && length()>0) {
            setCompoundDrawables(leftDrawable, null, right, null);
            isrighthow=true;
        }else if(!visible && length()<1){
            setCompoundDrawables(leftDrawable, null, null, null);
        }
    }



    @Override
    public void onFocusChange(View view, boolean b) {
        this.isFoucs = b;
        if (isFoucs) {
            clearText(getText().length() > 0);
            setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        }
        else
            clearText(false);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            int width = getWidth();
            int height = getHeight();
            int top = getTop();
            int left = getLeft();
            int rawx = (int) event.getRawX();
            int rawY = (int) event.getRawY();


            if(rawx<=left+width && rawx>=left && rawY>=top && rawY<=top+rawY) {

                //获取焦点
                setFocusable(true);
                setFocusableInTouchMode(true);
                requestFocus();

            }
            if (clearBtn != null && event.getAction() == MotionEvent.ACTION_UP) {
                int eventX = (int) event.getRawX();
                int eventY = (int) event.getRawY();
                Log.e("Log", "eventX = " + eventX + "; eventY = " + eventY);
                Rect rect = new Rect();
                getGlobalVisibleRect(rect);
                rect.left = rect.right - 50;
                if(rect.contains(eventX, eventY))
                    setText("");

                clearText(false);
            }

            //end_code
        }
        return super.onTouchEvent(event);
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        clearText(text.length() > 0);
        if(text.length()>0 && mRightListener!=null) {

                mRightListener.onTextChangeListener(text);

        }
    }

    @Override
    public void afterTextChanged(Editable editable) {


    }

    @Override
    public void onEditorAction(int actionCode) {

        super.onEditorAction(actionCode);
    }

    @Override
    public void setOnContextClickListener(OnContextClickListener l) {

        super.setOnContextClickListener(l);
    }


    public void setDrawableRightListener(DrawableRightListener listener) {

        this.mRightListener = listener;
    }




    public interface DrawableRightListener {

        public void onTextChangeListener(CharSequence text);

    }


}
